package com.woniu.tms.manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.entity.Admin;
import com.woniu.tms.entity.User;
import com.woniu.tms.manage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 跳转用户列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "WEB-INF/manage/user/index";
    }

//    /**
//     * 跳转修改用户页面
//     *
//     * @return
//     */
//    @RequestMapping("update")
//    public String update() {
//        return "WEB-INF/manage/user/update";
//    }

    /**
     * 跳转增加用户页面
     *
     * @return
     */
    @RequestMapping("add")
    public String add() {
        return "WEB-INF/manage/user/add";
    }

    /**
     * 展示所有用户列表
     *
     * @param pageNum
     * @param pageSize
     * @param condition
     * @return
     */
    @RequestMapping("indexDo")
    @ResponseBody
    public Map<String, Object> indexDo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       String condition, String rname) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<User> userList = userService.getByCondition(condition, rname, pageNum, pageSize);
            PageInfo<User> pageInfo = new PageInfo<>(userList);
            map.put("userList", userList);
            map.put("pageInfo", pageInfo);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }
        return map;
    }

//    /**
//     * 修改用户
//     *
//     * @param user
//     * @return
//     */
//    @RequestMapping("updateDo")
//    @ResponseBody
//    public Map<String, Object> updateDo(User user) {
//        Map<String, Object> map = new HashMap<>();
//        try {
//            userService.update(user);
//            map.put("result", true);
//        } catch (Exception e) {
//            map.put("result", false);
//            e.printStackTrace();
//        }
//        return map;
//    }

    /**
     * 增加用户
     *
     * @param user
     * @param rid
     * @return
     */
    @RequestMapping("addDo")
    @ResponseBody
    public Map<String, Object> addDo(User user, Integer rid) {
        Map<String, Object> map = new HashMap<>();
        try {
            userService.add(user, rid);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 删除删除单个用户
     *
     * @param uid
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public Map<String, Object> del(Integer uid) {
        Map<String, Object> map = new HashMap<>();
        try {
            userService.delete(uid);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 批量删除
     *
     * @param uids
     * @return
     */
    @RequestMapping("delAll")
    @ResponseBody
    public Map<String, Object> delAll(Integer[] uids) {
        Map<String, Object> map = new HashMap<>();
        try {
            userService.deleteAll(uids);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }
        return map;
    }

}
